{extend name='Common/index' /} {block name='content'}
<section class="content">
    <div class="box box-default">
        <div class="box-header with-border">
            <h3 class="box-title">{$info?'修改':'新增'}产品</h3>
        </div>
        <div class="box-body">
            <div class="form">
                <div class="form-group">
                    <label for="type" class="col-sm-2 control-label">产品分类</label>
                    <div class="col-sm-10">
                        <select name="type" id="type" class="form-control">
                          {foreach name="classlist" item="v"}
                          <option value="{$v.id}" {$info?$info['type'] == $v['id']?'selected':'':''}>{$v.title}</option>
                          {/foreach}
                        </select>
                        <input type="hidden" name="id" value="{$info?$info['id']:''}">
                    </div>
                </div>
                <div class="form-group">
                    <label for="name" class="col-sm-2 control-label">产品名称</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="name" name="name" placeholder="请输入产品名称" value="{$info?$info['name']:''}" autocomplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label for="group_id" class="col-sm-2 control-label">产品分组</label>
                    <div class="col-sm-10">
                        <select name="group_id" id="group_id" class="form-control">
                          {foreach name="groupList" item="v"}
                          <option value="{$v.id}" {$info?$info['group_id'] == $v['id'] ?'selected':'':''}>{$v.name}</option>
                          {/foreach}
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="describe" class="col-sm-2 control-label">产品描述</label>
                    <div class="col-sm-10">
                        <textarea name="describe" id="describe" class="form-control" cols="30" rows="10" placeholder="请输入产品描述">{$info?$info['describe']:''}</textarea>
                    </div>
                </div>
                <div class="form-group">
                    <label for="update_list" class="col-sm-2 control-label" multiple>可选升级套餐</label>
                    <div class="col-sm-10">
                        <select name="update_list" id="update_list" class="form-control" multiple style="height: 200px;">
                          {foreach name="updatelist" item="v"}
                          <option value="{$v.id}" {:in_array($v['id'],explode(',',$info['update_list']))?'selected':''}>{$v.name}</option>
                          {/foreach}
                        </select>
                        <span class="help-block">按住 Ctrl+Click 选择</span>
                    </div>
                </div>
                <div class="form-group">
                    <label for="email_model" class="col-sm-2 control-label">邮件模板ID</label>
                    <div class="col-sm-10">
                        <select name="email_model" id="email_model" class="form-control">
                          {foreach name="modelList" item="v"}
                          <option value="{$v.id}" {$info?$info['email_model'] == $v['id'] ?'selected':'':''}>{$v.name}</option>
                          {/foreach}
                        </select>
                    </div>
                </div>
                <div class="form-group">
                    <label for="label" class="col-sm-2 control-label">产品标签</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="label" name="label" placeholder="请输入产品标签" value="{$info?$info['label']:''}" autocomplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label for="month" class="col-sm-2 control-label">月价格</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="month" name="month" placeholder="请输入月价格" value="{$info?$info['month']:''}" autocomplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label for="quarter" class="col-sm-2 control-label">季度价格</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="quarter" name="quarter" placeholder="请输入季度价格" value="{$info?$info['quarter']:''}" autocomplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label for="semestrale" class="col-sm-2 control-label">半年价格</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="semestrale" name="semestrale" placeholder="请输入半年价格" value="{$info?$info['semestrale']:''}" autocomplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label for="years" class="col-sm-2 control-label">年价格</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="years" name="years" placeholder="请输入年价格" value="{$info?$info['years']:''}" autocomplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label for="biennium" class="col-sm-2 control-label">两年价格</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="biennium" name="biennium" placeholder="请输入两年价格" value="{$info?$info['biennium']:''}" autocomplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label for="triennium" class="col-sm-2 control-label">三年价格</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="triennium" name="triennium" placeholder="请输入三年价格" value="{$info?$info['triennium']:''}" autocomplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label for="sort" class="col-sm-2 control-label">产品排序</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="sort" name="sort" placeholder="请输入产品排序" value="{$info?$info['sort']:''}" autocomplete="off">
                    </div>
                </div>
                <div class="form-group">
                    <label for="plug" class="col-sm-2 control-label">插件接口</label>
                    <div class="col-sm-10">
                        <select name="plug" id="plug" class="form-control">
                          <option value="0">不使用插件</option>
                          {foreach name="addonslList" item="v"}
                          <option value="{$v.id}" {$info?$info['plug'] == $v['id'] ?'selected':'':''}>{$v.title} （接口范围：{$v.range} / 版本：{$v.version} / 作者：{$v.author}）</option>
                          {/foreach}
                        </select>
                    </div>
                </div>
                <div id="plugConfig">
                    {if $info['plug_config']} {$plug_config} {/if}
                </div>
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="button" class="btn btn-primary sub">提交</button>
                        <a href="javascript:history.go(-1);" class="btn btn-default">返回</a>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
{/block} {block name='js'}
<script src="__STATIC__/dist/js/formBasic.js"></script>
<script>
    $('.sub').click(function() {
        let names = document.querySelectorAll('.form-group > div > [name]'),
            vals = {};
        names = [].slice.call(names);
        names.map((e) => {
            vals[e.name] = $(e).val();
        });
        ajax('{:url("admin/Product/addAuth")}', vals, 'post').then(e => {
            if (e.status === 200) {
                alert(e.msg);
                location.href = "{:url('admin/Product/list')}";
            } else {
                alert(e.msg);
            }
        });
    });
    $('[name="plug"]').change(function() {
        let t = $(this);
        if (t.val() && t.val() != 0) {
            ajax('{:url("admin/Product/getPlugConfig")}?id=' + t.val()).then(e => {
                let data = JSON.parse(e.data)
                html = '';
                for (let i in data) {
                    switch (data[i].type) {
                        case 'text':
                            html += `<div class="form-group">
                                  <label for="${i}" class="col-sm-2 control-label">${i}</label>
                                  <div class="col-sm-10">
                                      <input type="${data[i].type}" class="form-control" id="${i}" name="${i}" placeholder="请输入${i}" value="${data[i].value}" autocomplete="off">
                                  </div>
                              </div>`;
                            break;
                        case 'password':
                            html += `<div class="form-group">
                                  <label for="${i}" class="col-sm-2 control-label">${i}</label>
                                  <div class="col-sm-10">
                                      <input type="${data[i].type}" class="form-control" id="${i}" name="${i}" placeholder="请输入${i}" value="${data[i].value}" autocomplete="off">
                                  </div>
                              </div>`;
                            break;
                        case 'select':
                            let opt = '';
                            data[i].opt.map(e => {
                                opt += `<option value="${e.value}" ${e.sel?'selected':''}>${e.name}</option>`;
                            });
                            html += `<div class="form-group">
                                  <label for="${i}" class="col-sm-2 control-label">${i}</label>
                                  <div class="col-sm-10">
                                    <select name="${i}" id="${i}" class="form-control">
                                      ${opt}
                                    </select>
                                  </div>
                              </div>`;
                            break;
                        case 'textarea':
                            html += `<div class="form-group">
                                  <label for="${i}" class="col-sm-2 control-label">${i}</label>
                                  <div class="col-sm-10">
                                    <textarea name="${i}" id="${i}" class="form-control" cols="30" rows="10" placeholder="请输入${i}">${data[i].value}</textarea>
                                  </div>
                              </div>`;
                            break;
                    }
                }
                $('#plugConfig').html(html);
            });
        } else {
            $('#plugConfig').html('');
        }

    });
</script>
{/block}